#该文件定义talk的基本类，供其他继承使用
#主要提供log方法...
import time
class base(object):
    #定义LOG等级
    LOG_DEBUG = 0
    LOG_INFO  = 1
    LOG_WARN  = 2
    LOG_ERROR = 3
    LOG_FATAL = 4

    def __init__(self):#logoutput如果不设置，则默认为python环境提供的print
        super(base,self).__init__()
        self.__logswitch = self.LOG_DEBUG
        self.logoutput = print
        self.__logname = 'Server'
        print('base init')

    def _setlogName(self,name):
        self.__logname = name

    def _getloglevelname(self,loglevel):
        if loglevel == self.LOG_DEBUG:
            return 'LOG DEBUG'
        elif loglevel == self.LOG_INFO:
            return 'LOG INFO'
        elif loglevel == self.LOG_WARN:
            return 'LOG WARN'
        elif loglevel == self.LOG_ERROR:
            return 'LOG ERROR'
        elif loglevel == self.LOG_FATAL:
            return 'LOG FATAL'
        else:
            raise(TypeError,'log level is out of range.')


    # 定义log，等级一般定义为：debug<info<warning<error<fatal
    #log开关设置为debug，则全部输出，设置为info，则>=info输出，其后类推
    def _log(self,logstr,loglevel =LOG_INFO): #如果输出等级小于输出开关，则不输出
        if loglevel >= self.__logswitch:
            self.logoutput('[%s %s %s]:'%(self.__logname,self._getloglevelname(loglevel),time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))) + logstr)

    def setlogswitch(self,loglevel):
        if loglevel>self.LOG_FATAL or loglevel<self.LOG_DEBUG:
            self._log('the loglevel you set is out of range.',self.LOG_ERROR)
            raise (TypeError, 'log level is out of range.')
        self.__logswitch = loglevel